package com.dream.testtubebaby.data;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.dream.testtubebaby.utils.DBHelper;

public class ToldItemDao {

	private DBHelper dbHelper;

	public ToldItemDao(Context context) {
		dbHelper = DBHelper.getInstance(context);
	}
//doctorid integer , fromtime text , totime text , stepname text , f_steptodo integer , 
//	drug text , howmuch text , f_finished integer , finishtime integer
	public void add(AddToldData newsItem) {
		String sql = "insert into tb_toldItem (doctorid,fromtime,totime,stepname,f_steptodo,drug,howmuch,f_finished,finishtime) values(?,?,?,?,?,?,?,?,?) ;";
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				sql,
				new Object[] { newsItem.getDoctorid(), newsItem.getFromtime(),
						newsItem.getTotime(), newsItem.getStepname(),
						newsItem.getF_steptodo(), newsItem.getDrug(),
						newsItem.getHowmuch(),newsItem.getF_finished(),newsItem.getFinishtime() });
		db.close();
	}

	public void deleteAll() {
		try {
			String sql = "delete from tb_toldItem";
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			db.execSQL(sql);
			db.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void add(List<AddToldData> newsItems) {
		for (AddToldData newsItem : newsItems) {
			add(newsItem);
		}
	}

	public List<AddToldData> list() {
		List<AddToldData> newsItems = new ArrayList<AddToldData>();
		try {
			String sql = "select doctorid,fromtime,totime,stepname,f_steptodo,drug,howmuch,f_finished,finishtime from tb_toldItem ";
			SQLiteDatabase db = dbHelper.getReadableDatabase();
			Cursor c = db.rawQuery(sql, null);

			AddToldData newsItem = null;

			while (c.moveToNext()) {
				newsItem = new AddToldData();

				long doctorid = c.getLong(0);
				String fromtime = c.getString(1);
				String totime = c.getString(2);
				String stepname = c.getString(3);
				int f_steptodo = c.getInt(4);
				String drug = c.getString(5);
				String howmuch = c.getString(6);
				int f_finished = c.getInt(7);
				long finishtime = c.getLong(8);

				newsItem.setDoctorid(doctorid);
				newsItem.setFromtime(fromtime);
				newsItem.setTotime(totime);
				newsItem.setStepname(stepname);
				newsItem.setF_steptodo(f_steptodo);
				newsItem.setDrug(drug);
				newsItem.setHowmuch(howmuch);
				newsItem.setF_finished(f_finished);
				newsItem.setFinishtime(finishtime);
				newsItems.add(newsItem);
			}
			c.close();
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newsItems;

	}

}
